import { loginApi, getUserInfoApi } from "@/services/my.js";
import {
  setAuthorization,
  removeAuthorization,
  getAuthorization,
} from "@/utils/auth.js";
export default {
  namespaced: true,
  state: {
    token: "",
    userInfo: {},
  },
  mutations: {
    setToken(state, value) {
      state.token = value;
      setAuthorization(value);
    },
    setUserInfo(state, value) {
      state.userInfo = value;
    },
    clearPermission(state) {
      state.token = "";
      removeAuthorization();
      state.userInfo = {};
    },
  },
  actions: {
    login(context, playload) {
      return new Promise((resolve, reject) => {
        loginApi(playload)
          .then((res) => {
            if (res.code == 1) {
              context.commit("setToken", res.data?.userinfo?.token);
              resolve(res);
            } else {
              reject(res.msg || "");
            }
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    getUserInfo(context) {
      return new Promise((resolve, reject) => {
        if (!getAuthorization()) {
          resolve({});
          return;
        }
        getUserInfoApi()
          .then((res) => {
            context.commit("setUserInfo", res.data.user_data || {});
			uni.setStorageSync('userId',res.data.user_data.id )
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};
